
#include "BitmapFont.h"

#include "../../SGD Wrappers/CSGD_TextureManager.h"
// Bitmap font done by David John
#include <cassert>
CBitmapFont::CBitmapFont(void)
{
	ImageID			= -1;
}

// DESTRUCTOR
CBitmapFont::~CBitmapFont(void)
{
}

// Initialize
//	- set up the bitmap font
void CBitmapFont::Initialize( void )
{
	// Load the image
	ImageID = CSGD_TextureManager::GetInstance()->LoadTexture(
		_T("../Resource/Graphics/letters.png") );
}

// Terminate
//	- unload resource
void CBitmapFont::Terminate( void )
{
	CSGD_TextureManager::GetInstance()->UnloadTexture( ImageID );
	ImageID = -1;
}

// Draw
//	- render the text, character-by-character using the image
void CBitmapFont::Draw( const TCHAR* strOutput, int nX, int nY,
					   float fScale, DWORD dwColor ) const
{
	// Validate the image
	assert( ImageID != -1 && "CBitmapFont::Draw - image was not loaded" );
	assert( strOutput != nullptr && "CBitmapFont::Draw - string cannot be null" );


	// Store the starting x location (for newlines)
	int nColStart = nX;
	RECT rCell;
	// Iterate through the string until reaching the null terminator
	for( int i = 0; strOutput[ i ]; i++ )
	{
		// Get the character from the array
		TCHAR ch = strOutput[ i ];
		rCell = CellAlgorithm( ch );
		if( ch == _T('\n') )
		{
			nY += (int)((rCell.bottom - rCell.top) * fScale);
			nX =  nColStart;
			continue;
		}
		else if( ch == _T('\t') )
		{
			// Calculate the number of characters printed
			int curr = (int)((nX - nColStart) / (rCell.bottom -rCell.top) * fScale);

			// Calculate the spaces to add
			int spaces = 4 - (curr % 4);


			// Move that many spaces
			nX += (int)(spaces * (rCell.right -rCell.left) * fScale);
			continue;
		}


		if(ch == TCTOLOWER( ch ))
		{
			ch = TCTOLOWER( ch );
		}
		else
		{
			ch = TCTOUPPER( ch );
		}

		// Draw the character
		CSGD_TextureManager::GetInstance()->Draw(
			ImageID, nX, nY, 
			fScale, fScale, &rCell,
			0.0f, 0.0f, 0.0f,
			dwColor );

		// Move to the next position
		nX +=  (int)((rCell.right -rCell.left) * fScale);
	}

}


// CellAlgorithm
//	- calculate the source rect for a cell
RECT CBitmapFont::CellAlgorithm( TCHAR ch) const
{
	RECT rCell = { };
	switch(ch)
	{
	case 32:
		{
			rCell.left = 4;	
			rCell.top = 0;	

			rCell.right = rCell.left + 4;	
			rCell.bottom = rCell.top + 44;
		}
		break;
	case 33:
		{
			rCell.left = 16;	
			rCell.top = 0;	

			rCell.right = rCell.left + 10;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 34:
		{
			rCell.left = 40;	
			rCell.top = 0;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 35:
		{
			rCell.left = 64;	
			rCell.top = 0;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 36:
		{
			rCell.left = 100;	
			rCell.top = 0;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 37:
		{
			rCell.left = 124;	
			rCell.top = 0;	

			rCell.right = rCell.left +30;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 38:
		{
			rCell.left = 156;	
			rCell.top = 0;	

			rCell.right = rCell.left +30;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 39:
		{
			rCell.left = 200;	
			rCell.top = 0;	

			rCell.right = rCell.left +10;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 40:
		{
			rCell.left = 212;	
			rCell.top = 0;	

			rCell.right = rCell.left +10;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 41:
		{
			rCell.left = 240;	
			rCell.top = 0;	

			rCell.right = rCell.left +10;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 42:
		{
			rCell.left = 256;	
			rCell.top = 0;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 43:
		{
			rCell.left = 292;	
			rCell.top = 0;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 44:
		{
			rCell.left = 324;	
			rCell.top = 0;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 45:
		{
			rCell.left = 342;	
			rCell.top = 0;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 46:
		{
			rCell.left = 370;	
			rCell.top = 0;	

			rCell.right = rCell.left +10;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 47:
		{
			rCell.left = 388;	
			rCell.top = 0;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 48:
		{
			rCell.left = 4;	
			rCell.top = 44;	

			rCell.right = rCell.left +18;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 49:
		{
			rCell.left = 32;	
			rCell.top = 44;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 50:
		{
			rCell.left = 64;	
			rCell.top = 44;	

			rCell.right = rCell.left +10;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 51:
		{
			rCell.left = 90;	
			rCell.top = 44;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 52:
		{
			rCell.left = 118;	
			rCell.top = 44;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 53:
		{
			rCell.left = 148;	
			rCell.top = 44;	

			rCell.right = rCell.left +18;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 54:
		{
			rCell.left = 178;	
			rCell.top = 44;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 55:
		{
			rCell.left = 208;	
			rCell.top = 44;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 56:
		{
			rCell.left = 236;	
			rCell.top = 44;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 57:
		{
			rCell.left = 266;	
			rCell.top = 44;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 58:
		{
			rCell.left = 296;	
			rCell.top = 44;	

			rCell.right = rCell.left +10;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 59:
		{
			rCell.left = 316;	
			rCell.top = 44;	

			rCell.right = rCell.left +10;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 60:
		{
			rCell.left = 336;	
			rCell.top = 44;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 61:
		{
			rCell.left = 363;	
			rCell.top = 44;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 62:
		{
			rCell.left = 393;	
			rCell.top = 44;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 63:
		{
			rCell.left = 420;	
			rCell.top = 44;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 64:
		{
			rCell.left = 4;	
			rCell.top = 88;	

			rCell.right = rCell.left +30;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 65:
		{
			rCell.left = 45;	
			rCell.top = 88;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 66:
		{
			rCell.left = 80;	
			rCell.top = 88;	

			rCell.right = rCell.left +12;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 67:
		{
			rCell.left = 110;	
			rCell.top = 88;	

			rCell.right = rCell.left +12;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 68:
		{
			rCell.left = 135;	
			rCell.top = 88;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 69:
		{
			rCell.left = 170;	
			rCell.top = 88;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 70:
		{
			rCell.left = 200;	
			rCell.top = 88;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 71:
		{
			rCell.left = 228;	
			rCell.top = 88;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 72:
		{
			rCell.left = 260;	
			rCell.top = 88;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 73:
		{
			rCell.left = 286;	
			rCell.top = 88;	

			rCell.right = rCell.left +12;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 74:
		{
			rCell.left = 310;	
			rCell.top = 88;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 75:
		{
			rCell.left = 335;	
			rCell.top = 88;	

			rCell.right = rCell.left +18;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 76:
		{
			rCell.left = 367;	
			rCell.top = 88;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 77:
		{
			rCell.left = 397;	
			rCell.top = 88;	

			rCell.right = rCell.left +26;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 78:
		{
			rCell.left = 432;	
			rCell.top = 88;	

			rCell.right = rCell.left +22;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 79:
		{
			rCell.left = 467;	
			rCell.top = 88;	

			rCell.right = rCell.left +24;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 80:
		{
			rCell.left = 4;	
			rCell.top = 132;	

			rCell.right = rCell.left +18;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 81:
		{
			rCell.left = 36;	
			rCell.top = 132;	

			rCell.right = rCell.left +18;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 82:
		{
			rCell.left = 70;	
			rCell.top = 132;	

			rCell.right = rCell.left +12;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 83:
		{
			rCell.left = 96;	
			rCell.top = 132;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 84:
		{
			rCell.left = 124;	
			rCell.top = 132;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 85:
		{
			rCell.left = 156;	
			rCell.top = 132;	

			rCell.right = rCell.left +18;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 86:
		{
			rCell.left = 190;	
			rCell.top = 132;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 87:
		{
			rCell.left = 220;	
			rCell.top = 132;	

			rCell.right = rCell.left +24;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 88:
		{
			rCell.left = 258;	
			rCell.top = 132;	

			rCell.right = rCell.left +18;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 89:
		{
			rCell.left = 288;	
			rCell.top = 132;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 90:
		{
			rCell.left = 316;	
			rCell.top = 132;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 91:
		{
			rCell.left = 342;	
			rCell.top = 132;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 92:
		{
			rCell.left = 370;	
			rCell.top = 132;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 93:
		{
			rCell.left = 396;	
			rCell.top = 132;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 94:
		{
			rCell.left = 414;	
			rCell.top = 132;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 95:
		{
			rCell.left = 448;	
			rCell.top = 132;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
	case 96:
		{
			rCell.left = 4;	
			rCell.top = 176;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 97:
		{
			rCell.left = 28;	
			rCell.top = 176;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 98:
		{
			rCell.left = 58;	
			rCell.top = 176;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 99:
		{
			rCell.left = 86;	
			rCell.top = 176;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 100:
		{
			rCell.left = 114;	
			rCell.top = 176;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 101:
		{
			rCell.left = 144;	
			rCell.top = 176;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 102:
		{
			rCell.left = 170;	
			rCell.top = 176;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 103:
		{
			rCell.left = 196;	
			rCell.top = 176;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 104:
		{
			rCell.left = 224;	
			rCell.top = 176;	

			rCell.right = rCell.left +12;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 105:
		{
			rCell.left = 252;	
			rCell.top = 176;	

			rCell.right = rCell.left +12;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 106:
		{
			rCell.left = 272;	
			rCell.top = 176;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 107:
		{
			rCell.left = 296;	
			rCell.top = 176;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 108:
		{
			rCell.left = 324;	
			rCell.top = 176;	

			rCell.right = rCell.left +10;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 109:
		{
			rCell.left = 346;	
			rCell.top = 176;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 110:
		{
			rCell.left = 380;	
			rCell.top = 176;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 111:
		{
			rCell.left = 410;	
			rCell.top = 176;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 112:
		{
			rCell.left = 4;	
			rCell.top = 220;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 113:
		{
			rCell.left = 34;	
			rCell.top = 220;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 114:
		{
			rCell.left = 64;	
			rCell.top = 220;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 115:
		{
			rCell.left = 92;	
			rCell.top = 220;	

			rCell.right = rCell.left +12;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 116:
		{
			rCell.left = 118;	
			rCell.top = 220;	

			rCell.right = rCell.left +12;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 117:
		{
			rCell.left = 142;	
			rCell.top = 220;	

			rCell.right = rCell.left +12;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 118:
		{
			rCell.left = 168;	
			rCell.top = 220;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 119:
		{
			rCell.left = 198;	
			rCell.top = 220;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 120:
		{
			rCell.left = 231;	
			rCell.top = 220;	

			rCell.right = rCell.left +14;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 121:
		{
			rCell.left = 260;	
			rCell.top = 220;	

			rCell.right = rCell.left +16;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 122:
		{
			rCell.left = 288;	
			rCell.top = 220;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 123:
		{
			rCell.left = 312;	
			rCell.top = 220;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 124:
		{
			rCell.left = 336;	
			rCell.top = 220;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 125:
		{
			rCell.left = 364;	
			rCell.top = 220;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 126:
		{
			rCell.left = 390;	
			rCell.top = 220;	

			rCell.right = rCell.left +20;	
			rCell.bottom = rCell.top +44;
		}
		break;
		case 127:
		{
			rCell.left = 420;	
			rCell.top = 220;	

			rCell.right = rCell.left;	
			rCell.bottom = rCell.top;
		}
		break;
	}

	return rCell;
}

